using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DCApp
{
    public partial class EnrollDlg : Form
    {

        string _appSpecificParticipantId;
        public string AppSpecificParticpantId
        {
            get
            {
                return _appSpecificParticipantId;
            }
            set
            {
                _appSpecificParticipantId = value;
            }
        }
        

        public EnrollDlg()
        {
            InitializeComponent();
            
        }


        private void Form_Load(object sender, EventArgs e)
        {
            //Create a random value for appliction specific participant id
            //The user can further edit it anyway
            this.txtAppSpecificParticipantId.Text = System.Guid.NewGuid().ToString();
        }

      

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string applicationSpecificParticipantId = txtAppSpecificParticipantId.Text.Trim();
            string participantName = txtParticipantName.Text.Trim();
            string securityQuestion = txtSecurityQuestion.Text.Trim();
            string securityAnswer = txtSecurityAnswer.Text.Trim();

            if (
                String.IsNullOrEmpty(applicationSpecificParticipantId) ||
                String.IsNullOrEmpty(participantName) ||
                String.IsNullOrEmpty(securityQuestion) ||
                String.IsNullOrEmpty(securityAnswer))
            {
                MessageBox.Show("Please enter values for all fields");
                return;
            }


            try
            {
                //Call HealthVault and obtain a particpantCode for this participant
                string participantCode = DCHelper.Functions.CreateParticipantIdentityCode(participantName,
                    securityQuestion,
                    securityAnswer,
                    applicationSpecificParticipantId);

                //Update the local data storage with this new participant
                XmlStore.AddParticipantToLocalStore(applicationSpecificParticipantId,
                    participantName,
                    DateTime.Now,
                    participantCode,
                    securityQuestion,
                    securityAnswer);

                this.DialogResult = DialogResult.OK;
                this.Close();

                //Set the application specific id of the participant just created
                //to this dialog's member variable so that the calling form can use it
                AppSpecificParticpantId = applicationSpecificParticipantId;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Exception:" + ex.Message);
            }

        }
    }
}